<?php


namespace app\tt\controller;

use app\common\model\MiniApp;
use app\http\middleware\checkTTUserToken;
use app\user\logic\LoginMiniProgram;
use app\user\model\TtUser;
use EasyWeChat\Factory;
use think\Controller;
use think\Db;
use think\facade\Config;
use think\facade\Request;

class Login extends Controller
{

    //返回字节跳动在本平台登录的token
    private function get_tt_token($openid)
    {
        $user = TtUser::where(['openid' => $openid])->find();
        $data = [
            "token" => checkTTUserToken::issueToken($user)
        ];
        return $data;
    }

    public function wx_login()
    {
        $code = Request::param('code');

        $platform = Request::param('platform', 'wx');       //用户的id
        $mini_app_code = Request::param('mini_app_code', 'test');       //用户的id

        $mini_app=MiniApp::where('code',$mini_app_code)->find();

        $appid =$mini_app->wx_appid;
        $secret = $mini_app->wx_secret;

        //平台的api接口
        $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
        $info = json_decode(httpGet($api));

        //字节平台的openid
        $openid = $info->openid;

        //判断账号是否注册
        $ttuser = TtUser::where(['openid' => $openid])->find();

        //账号已经注册
        if ($ttuser) {
            $this->result($this->get_tt_token($openid), 200, '登录成功');
        } else {

            //账号未注册
            $res = TtUser::create([
                'user_id' => 0,
                'platform' => 'wx',
                'openid' => $openid,
                'add_time' => time()
            ]);
            if($res){
                $this->result($this->get_tt_token($openid), 200, '登录成功');
            }else{
                $this->result(null, 300, '登录失败，请重试');
            }
        }
    }

    //头条登录
    public function login()
    {
        $code = Request::param('code');

        $platform = Request::param('platform', 'dy');       //用户的id

        $appid = Config::get('miniprogram.tt.appid');
        $secret = Config::get('miniprogram.tt.secret');

        //平台的api接口
        $api = "http://developer.toutiao.com/api/apps/jscode2session?appid={$appid}&secret={$secret}&code={$code}";
        $info = json_decode(httpGet($api));

        //字节平台的openid
        $openid = $info->openid;

        //判断账号是否注册
        $ttuser = TtUser::where(['openid' => $openid])->find();

        //账号已经注册
        if ($ttuser) {
            $this->result($this->get_tt_token($openid), 200, '登录成功');
        } else {

            //账号未注册
            $res = TtUser::create([
                'user_id' => 0,
                'platform' => 'dy',
                'openid' => $openid,
                'add_time' => time()
            ]);
            if($res){
                $this->result($this->get_tt_token($openid), 200, '登录成功');
            }else{
                $this->result(null, 300, '登录失败，请重试');
            }
        }
    }

}
